<template>
  <div class="app-container home">
    <div class="fr">
      <el-form :inline="true" :model="formInline" class="">
        <el-form-item label="关键词" prop="keyword">
          <el-input v-model="formInline.keyword" clearable placeholder="请输入" size="small"></el-input>
        </el-form-item>
        <el-form-item label="变量编码" prop="variableCode">
          <el-input v-model="formInline.variableCode" clearable placeholder="请输入" size="small"></el-input>
        </el-form-item>
        <el-form-item label="变量名空间" prop="variableNamespace">
          <el-input v-model="formInline.variableNamespace" clearable placeholder="请输入" size="small"></el-input>
        </el-form-item>
        <el-form-item label="变量类型" prop="variableType">
          <el-select v-model="formInline.variableType" clearable placeholder="请选择" size="small">
            <el-option v-for="(item,index) in variableTypeList" :key="'variable-type'+index" :value="item.type"
              :label="item.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch" size="small">搜索</el-button>
          <el-button type="primary" plain @click="handleReset" size="small">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="btn">
      <el-button type="primary" size="small" @click="handleAdd">新增</el-button>
    </div>
    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="序号" type="index" width="50" :index="indexMethod">
      </el-table-column>
      <el-table-column prop="variableCode" label="变量编码"> </el-table-column>
      <el-table-column prop="variableDescribe" label="变量描述"> </el-table-column>
      <el-table-column prop="variableName" label="变量名称" />
      <el-table-column prop="variableNamespace" label="变量名空间" />
      <el-table-column prop="variableType" label="变量类型" />
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="handleView(scope.row)">查看</el-button>
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination class="p20 fr" v-if="total > 0" :background="true" :current-page.sync="queryParams.pageNum"
      :page-size.sync="queryParams.pageSize" :layout="'total, sizes, prev, pager, next, jumper'"
      :page-sizes="[10, 30, 50, 100]" :total="total" @size-change="handleSizeChange"
      @current-change="handlepageNumChange" />
    <VariableDialog :datas="variableDatas" v-if="dialogVisible" :type="type" @on-close="handleDialogClose"
      @on-sure="onSearch" />
  </div>
</template>

<script>
  import VariableDialog from '@/components/Editor/variableDialog.vue'
  import { getDocumentVariableList, deleteDocumentVariable,getVariableTypeList } from "@/api/variable";
  export default {
    name: "VariableConfig",
    components: { VariableDialog },
    data() {
      return {
        formInline: {
          keyword: "",
          variableCode: "",
          variableNamespace: '',
          variableType: ''
        },
        queryParams: {
          pageNum: 1,
          pageSize: 10,
        },
        loading: false,
        total: 0,
        tableData: [],
        dialogVisible: false,
        variableDatas: {},
        type: 'add',
        variableTypeList: []
      };
    },
    methods: {
      indexMethod(index) {
        return (
          (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1
        );
      },
      getOptions() {
        getVariableTypeList().then(res => {
          this.variableTypeList = res.data
        })
      },
      async onSearch() {
        try {
          this.loading = true;
          const res = await getDocumentVariableList({
            ...this.queryParams,
            ...this.formInline,
          });
          this.tableData = res.data.list;
          this.total = res.data.count;
        } catch (error) {
          console.debug(error);
        } finally {
          this.loading = false;
        }
      },
      handleReset() {
        this.formInline = {
          keyword: "",
          variableCode: "",
          variableNamespace: '',
          variableType: ''
        };
        this.onSearch();
      },
      handleSearch() {
        this.queryParams.pageNum = 1;
        this.onSearch();
      },
      handleSizeChange(val) {
        if (this.queryParams.pageSize * val > this.total) {
          this.queryParams.pageNum = 1;
        }
        this.onSearch();
      },
      handlepageNumChange() {
        this.onSearch();
      },
      handleAdd() {
        this.variableDatas = {}
        this.type = 'add'
        this.dialogVisible = true
      },
      handleView(row) {
        this.variableDatas = row
        this.type = 'view'
        this.dialogVisible = true
      },
      handleEdit(row) {
        this.variableDatas = row
        this.type = 'edit'
        this.dialogVisible = true
      },
      handleDelete(id) {
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteDocumentVariable(id).then(res => {
            this.$message({ type: 'success', message: '删除成功!' });
            this.handleSearch();
          })
        }).catch(() => {
          this.$message({ type: 'info', message: '已取消删除' });
        });
      },
      handleDialogClose() {
        this.dialogVisible = false
      }
    },
    mounted() {
      this.handleSearch();
      this.getOptions();
    },
  };
</script>
<style scoped>
  .btn {
    padding: 16px;
  }
</style>